package com.znf4.banklink.alipay;
/*package com.znf4.platform.banklink.alipay;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bigpay.bank.enums.BankWayEnum;
import com.bigpay.banklink.enums.BankLinkNetWorkStatusEnum;
import com.bigpay.banklink.facade.CnpPayFacade;
import com.bigpay.banklink.facade.CnpPayVerifyFacade;
import com.bigpay.banklink.utils.longyang.LongYangD0Util;
import com.bigpay.banklink.vo.CnpPayPreParam;
import com.bigpay.banklink.vo.CnpPayPreResult;
import com.bigpay.banklink.vo.CnpPayQueryParam;
import com.bigpay.banklink.vo.CnpPayQueryResult;
import com.bigpay.banklink.vo.NotifyVerifyParam;
import com.bigpay.banklink.vo.NotifyVerifyResult;
import com.bigpay.common.core.enums.PayWayEnum;
import com.bigpay.common.core.utils.DateUtils;
import com.bigpay.common.core.utils.StringUtil;
import com.bigpay.notify.service.RpNotifyService;
import com.bigpay.trade.dao.RpTradePaymentRecordDao;
import com.bigpay.trade.entity.RpTradePaymentRecord;
import com.bigpay.trade.enums.TradeStatusEnum;
import com.bigpay.user.entity.RpUserSecondLevelMerchant;
import com.bigpay.user.entity.RpUserSecondLevelMerchantRelation;
import com.bigpay.user.entity.RpUserSecondLevelMerchantTradeRecord;
import com.bigpay.user.exception.UserBizException;
import com.bigpay.user.service.RpUserSecondLevelMerchantRelationService;
import com.bigpay.user.service.RpUserSecondLevelMerchantService;
import com.bigpay.user.service.RpUserSecondLevelMerchantTradeRecordService;
import com.bigpay.user.vo.SumSecondRecordVo;
*//**
 * 龙飏T0支付宝扫码支付
 *//*
@Service("ALIPAY_SCANPAY_LONG_YANG_D0")
public class LongYangAliPayScanPayD0Impl implements CnpPayFacade, CnpPayVerifyFacade {
	
	private static final Logger LOG = LoggerFactory.getLogger(LongYangAliPayScanPayD0Impl.class);
	
	@Autowired
	private RpUserSecondLevelMerchantRelationService rpUserSecondLevelMerchantRelationService;
	
	@Autowired
	private RpUserSecondLevelMerchantTradeRecordService rpUserSecondLevelMerchantTradeRecordService;
	
	@Autowired
	private RpUserSecondLevelMerchantService rpUserSecondLevelMerchantService;
	
	@Autowired
	private RpTradePaymentRecordDao rpTradePaymentRecordDao ;
	
	@Autowired
	private RpNotifyService rpNotifyService;
	
	@Override
	public NotifyVerifyResult cnpPayVerify(NotifyVerifyParam notifyVerifyParam) {
		LOG.info("进入..龙飏支付宝扫码通知回调");
		
		NotifyVerifyResult notifyVerifyResult = new NotifyVerifyResult();
		notifyVerifyResult.setBankChannelCode(notifyVerifyParam.getBankChannelCode());  // 银行渠道编码
		Map<String, Object> paramMap = notifyVerifyParam.getParamMap() ;  // 获取  参数 
		System.out.println("龙飏 支付宝 回调了我们的接口 ----- 获取到 的 map 中的数据 ：" + paramMap );
		String orderId = (String) paramMap.get("orderId") ;   // 订单号
		String respCode = (String) paramMap.get("respCode") ; // 返回码
		//String respInfo = (String) paramMap.get("respInfo") ; // 返回码描述
		String amount = (String) paramMap.get("amount") ;     // 交易金额
		//String WXOrderNo = (String) paramMap.get("WXOrderNo") ;  // 微信订单号
		
		//获取商户订单号
		RpTradePaymentRecord  rpTradePaymentRecord =  rpTradePaymentRecordDao.getByBankTrxNo(orderId) ;
		
		if(null == rpTradePaymentRecord ){
			throw  new RuntimeException("龙飏扫码支付流水订单号不存在！") ;
		}
		
		
		RpUserSecondLevelMerchantTradeRecord rpUserSecondLevelMerchantTradeRecord = rpUserSecondLevelMerchantTradeRecordService.selectByBankOrderNo(rpTradePaymentRecord.getBankOrderNo()); //查询二级商户号
		
		notifyVerifyResult.setIsVerify(true);// 验签成功
		if ("000000".equals( respCode )) {
			notifyVerifyResult.setTradeStatusEnum(TradeStatusEnum.SUCCESS);// 支付成功
			notifyVerifyResult.setBankOrderNo( rpTradePaymentRecord.getBankOrderNo() );  // 银行订单号
			notifyVerifyResult.setPayAmount(BigDecimal.valueOf(Double.valueOf(amount)).divide(BigDecimal.valueOf(100l)));// 支付金额
			notifyVerifyResult.setResponseStr(paramMap.toString());
			notifyVerifyResult.setBankSuccessTime(new Date());
			notifyVerifyResult.setResponseStr("SUCCESS");
			
			rpUserSecondLevelMerchantTradeRecord.setStatus(TradeStatusEnum.SUCCESS.name());
		} else {
			notifyVerifyResult.setTradeStatusEnum(TradeStatusEnum.FAILED);// 支付失败
			
			rpUserSecondLevelMerchantTradeRecord.setStatus(TradeStatusEnum.FAILED.name());
		}
		
		rpUserSecondLevelMerchantTradeRecordService.updateData(rpUserSecondLevelMerchantTradeRecord); //修改二级商户交易
		
		return notifyVerifyResult;
	}
	
	

	*//**
	 * 初始化支付,通过该方法获取扫码支付链接.
	 *//*
	@Override
	public CnpPayPreResult cnpPay(CnpPayPreParam cnpPayPreParam) {
		LOG.info("进入..初始化支付,通过该方法获取支付链接");
		String bankDesc = cnpPayPreParam.getBankDesc(); //商户银行通道附属信息
		if (StringUtil.isEmpty(bankDesc)) {
			bankDesc = "龙飏支付";
		}
		
		String tradeAmount = String.valueOf(cnpPayPreParam.getPayAmount().multiply(BigDecimal.valueOf(100l)).intValue());
		System.out.println("  支付的金额是  （分）： " + tradeAmount);
		
        //商户池中二级商户的切换:这里采用规则选择合适的二级商户进行支付处理
		//根据当前一级商户来查询对应关联的二级商户
		LOG.info("正在交易的商户为:【"+cnpPayPreParam.getMerchantNo()+"】");
		
		RpUserSecondLevelMerchant secondMerchant = searchSecondMerchant(cnpPayPreParam); //查找通道商户编号（更新时间）
		
		String account = secondMerchant.getBusiContactMobile(); //获取用户支付的账户
		String PRIVATEKEY =  secondMerchant.getBankMerchantSecret(); //获取支付密钥
		
		LOG.info("account:["+account+"]  "+"PRIVATEKEY:["+PRIVATEKEY+"]");
		
		String tradeCode = "tb_WeixinPay";
		String channel_code = "ALIPAY";
		
		Map<String, String> resultMap = LongYangD0Util.alipayOrWxScanPayLink(tradeCode, account, tradeAmount, bankDesc,
				channel_code, PRIVATEKEY);
		
		//创建二级商户交易记录实体
		RpUserSecondLevelMerchantTradeRecord rpUserSecondLevelMerchantTradeRecord = new RpUserSecondLevelMerchantTradeRecord();
		rpUserSecondLevelMerchantTradeRecord.setSecondLevelMerchantNo(secondMerchant.getSecondLevelMerchantNo()); //二级商户编号
		rpUserSecondLevelMerchantTradeRecord.setMerchantNo(cnpPayPreParam.getMerchantNo());
		rpUserSecondLevelMerchantTradeRecord.setBankOrderNo(cnpPayPreParam.getBankOrderNo());
		rpUserSecondLevelMerchantTradeRecord.setOrderAmount(cnpPayPreParam.getPayAmount());
		
		// 处理响应
		CnpPayPreResult result1 = new CnpPayPreResult();
		if ("000000".equals(resultMap.get("retCode")) || "500001".equals(resultMap.get("retCode")) ) {
			// 交易成功,或支付中
			result1.setBankLinkNetWorkStatusEnum(BankLinkNetWorkStatusEnum.SUCCESS);
			result1.setBankReturnMsg(resultMap.get("retMsg"));
			result1.setPayMessage(resultMap.get("QRcodeURL"));

			String orderId = resultMap.get("orderId"); // 上游返回的订单流水标号 。
			System.out.println("龙飏返回来订单ID ：" + orderId);
			*//** 将龙飏 微信 d0 扫码支付，返回的订单保存到我们的数据库中 *//*
			result1.setBankTrxNo(orderId);
			
			rpUserSecondLevelMerchantTradeRecord.setStatus(TradeStatusEnum.WAITING_PAYMENT.name()); //修改二级商户交易状态-等待支付
			
		} else {
			LOG.error("龙飏-微信D0扫码支付-报文出错：" + resultMap);
			result1.setBankLinkNetWorkStatusEnum(BankLinkNetWorkStatusEnum.FAILED);
			result1.setBankReturnMsg(resultMap.get("respDesc"));
			
			rpUserSecondLevelMerchantTradeRecord.setStatus(TradeStatusEnum.UNKNOWN.name()); //请求报文出错-修改二级商户状态-未知错误
		}
		
		rpUserSecondLevelMerchantTradeRecordService.saveData(rpUserSecondLevelMerchantTradeRecord); //保存二级商户交易记录
		
		return result1;
	}

	
	*//**
	 * 支付宝支付交易查询接口
	 *
	 * @param cnpPayQueryParam
	 * @return
	 *//*
	@Override
	public CnpPayQueryResult cnpPayQuery(CnpPayQueryParam cnpPayQueryParam) {
		LOG.info("进入..支付宝支付交易查询接口");
		
		RpUserSecondLevelMerchantTradeRecord rpUserSecondLevelMerchantTradeRecord = rpUserSecondLevelMerchantTradeRecordService.selectByBankOrderNo(cnpPayQueryParam.getBankOrderNo()); //查询二级商户号
		String subMchId = rpUserSecondLevelMerchantTradeRecord.getSecondLevelMerchantNo(); //查询到这笔交易关联的二级商户编号
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("secondLevelMerchantNo", subMchId);
		RpUserSecondLevelMerchant secondMerchant = rpUserSecondLevelMerchantService.getBy(paramMap); //根据二级商户编号查询二级商户信息
		
		String account = secondMerchant.getBusiContactMobile(); //查询上游的账户号
		String PRIVATEKEY =  secondMerchant.getBankMerchantSecret(); //获取支付密钥
		
		LOG.info("account:["+account+"]  "+"PRIVATEKEY:["+PRIVATEKEY+"]");
		
		String tradeCode = "tb_OrderConfirm";
		
		Map<String, Object > map = LongYangD0Util.orderConfirm(tradeCode, account ,  cnpPayQueryParam.getBankTrxNo() , PRIVATEKEY );
		
		CnpPayQueryResult result = new CnpPayQueryResult();
		result.setBankChannelCode(cnpPayQueryParam.getBankChannelCode());
		result.setBankOrderNo(cnpPayQueryParam.getBankOrderNo());
		result.setTradeStatusEnum(TradeStatusEnum.UNKNOWN);
		result.setBankSuccessTime(new Date());
		
		result.setBankTrxNo((String)map.get("orderId"));
		if ("000000".equals(map.get("respCode"))) {
			// 查询成功
			if ("000000".equals(map.get("retCode"))) {
				// 交易成功
				result.setTradeStatusEnum(TradeStatusEnum.SUCCESS);
				
				rpUserSecondLevelMerchantTradeRecord.setStatus(TradeStatusEnum.SUCCESS.name());
				
			} else if ("1006".equals(map.get("retCode"))) {
				LOG.info("支付宝支付交易查询接口 : 验签失败，从新发起查询。");
				result.setTradeStatusEnum(TradeStatusEnum.WAITING_PAYMENT);
				
				rpUserSecondLevelMerchantTradeRecord.setStatus(TradeStatusEnum.UNKNOWN.name());
			}
			
		}else if ("500001".equals(map.get("retCode"))) {
			// 交易处理中
			result.setTradeStatusEnum(TradeStatusEnum.WAITING_PAYMENT);
			
			rpUserSecondLevelMerchantTradeRecord.setStatus(TradeStatusEnum.WAITING_PAYMENT.name());
		}
		
		rpUserSecondLevelMerchantTradeRecordService.updateData(rpUserSecondLevelMerchantTradeRecord); //修改二级商户交易
		return result;
		
	}
	
	
	*//**
	 * 查找通道商户编号（更新时间）
	 * @param cnpPayPreParam
	 * @return
	 *//*
	private RpUserSecondLevelMerchant searchSecondMerchant(CnpPayPreParam cnpPayPreParam) {
		Map<String, Object> merchantMap = new HashMap<String, Object>();
		merchantMap.put("merchantNo", cnpPayPreParam.getMerchantNo());
		merchantMap.put("orderAmount", cnpPayPreParam.getPayAmount());
		merchantMap.put("orderTime", DateUtils.getHour(new Date()));
		merchantMap.put("payWayCode", PayWayEnum.ALIPAY.name());
		merchantMap.put("bankWayCode", BankWayEnum.LONGYANG.name());
		merchantMap.put("merchantSize", 30);
		
		List<String> isSecondLevelMerchantNo = new ArrayList<String>();
		Map<String, Object> params = new HashMap<String, Object>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String billDateStr = sdf.format(new Date());
		params.put("billDate", billDateStr);
		
		//统计二级商户交易大于3的记录-查询当天的
		List<SumSecondRecordVo> recordList = rpUserSecondLevelMerchantTradeRecordService.countRecord10(params);
		for(SumSecondRecordVo item : recordList){
			isSecondLevelMerchantNo.add(item.getSecondLevelMerchantNo());
		}
		if(!isSecondLevelMerchantNo.isEmpty()){
			merchantMap.put("isSecondLevelMerchantNo", isSecondLevelMerchantNo);
		}
		//查询商户和二级商户关联表
		List<RpUserSecondLevelMerchantRelation> relationList = rpUserSecondLevelMerchantRelationService.listBy(merchantMap);
		if(relationList.isEmpty()){
			throw new UserBizException(UserBizException.USER_IS_NULL, "检索二级商户为空！");
		}
		//从查询的关联表数据中随机选择一条来支付
		Random random=new Random();
		int result=random.nextInt(relationList.size());
		RpUserSecondLevelMerchantRelation relation = relationList.get(result);
		
		//根据关联表中的二级商户编号来查询二级商户的具体信息
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("secondLevelMerchantNo", relation.getSecondLevelMerchantNo());
		RpUserSecondLevelMerchant secondMerchant = rpUserSecondLevelMerchantService.getBy(paramMap);
//		secondMerchant.setScore(secondMerchant.getScore()-1);
//		rpUserSecondLevelMerchantService.updateData(secondMerchant);
		
		//更新时间
		
		relation.setUpdateTime(new Date());
		rpNotifyService.updateSecondLevelMerchantTrade(relation); //修改二级商户关系-(将当前使用商户-二级商户关联表数据发送到mq),这里是正要使用的放mq
		return secondMerchant;
	}
	
	
}
*/